package com.wanxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.CompanyModel;
import com.wanxi.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Li
 * @Date 2023/7/8 20:45
 */
@RestController
@Api(tags = "公司信息管理", value = "用户OpenApi入口")
@RequestMapping("/back/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @GetMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询公司信息", response = CompanyModel.class, httpMethod = "GET")
    public ResultDto findAll(CompanyModel companyModel) {
        Page<CompanyModel> page = new Page<>(companyModel.getPage(), companyModel.getLimit());
        QueryWrapper<CompanyModel> queryWrappers = query(companyModel);
        IPage<CompanyModel> iPage = companyService.page(page, queryWrappers);
        return ResultDto.builder()
                .code(0)
                .data(iPage.getRecords())
                .count((int) iPage.getTotal())
                .build();
    }


    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "根据id查询公司信息", response = CompanyModel.class, httpMethod = "GET")
    public ResultDto findById(CompanyModel companyModel) {
        CompanyModel company = companyService.getById(companyModel.getId());
        return ResultDto.builder()
                .code(0)
                .data(company)
                .build();
    }

    @PostMapping("addCompany")
    @PreAuthorize("hasAnyAuthority('add')")//权限
    @ApiOperation(value = "新增公司信息", response = CompanyModel.class, httpMethod = "POST")
    public ResultDto addCompany(CompanyModel companyModel) {
        companyService.save(companyModel);
        return ResultDto.builder()
                .code(0)
                .msg("新增成功！")
                .build();
    }

    @PostMapping("/updateCompany")
    @PreAuthorize("hasAnyAuthority('update')")//权限
    @ApiOperation(value = "修改公司信息", response = CompanyModel.class, httpMethod = "POST")
    public ResultDto updateCompany(CompanyModel companyModel) {
        companyService.updateById(companyModel);
        return ResultDto.builder()
                .code(0)
                .msg("修改成功！")
                .build();
    }

    @PostMapping("/deleteCompany")
    @PreAuthorize("hasAnyAuthority('delete')")//权限
    @ApiOperation(value = "删除公司信息", response = CompanyModel.class, httpMethod = "POST")
    public ResultDto deleteCompany(CompanyModel companyModel) {
        companyService.removeById(companyModel.getId());
        return ResultDto.builder()
                .code(0)
                .msg("删除成功！")
                .build();
    }

    private QueryWrapper<CompanyModel> query(CompanyModel companyModel) {
        QueryWrapper<CompanyModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(companyModel.getName() == null), "name", companyModel.getName());
        queryWrapper.like(!(companyModel.getAddress() == null), "address", companyModel.getAddress());
        return queryWrapper;
    }
}
